package com.loup.proxy;

import com.loup.registory.MapperRegistory;
import com.loup.session.LoupSqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by loup on 2018/4/26.
 */
public class LoupMapperProxy<T>  implements InvocationHandler{

    private LoupSqlSession sqlSession;

    public LoupMapperProxy(LoupSqlSession defaultLoupSqlSession){
        this.sqlSession = defaultLoupSqlSession;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //要执行sql，就是要拿到sqlsession去执行对应的操作，
//        String sql = MapperXml.getSqlMappering(method.getDeclaringClass().getName(),method.getName());
        MapperRegistory.MapperData mapperData = MapperRegistory.getMapperData(method.getDeclaringClass().getName()+"."+method.getName());
        if (mapperData != null && mapperData.getSql()!=null){
            return sqlSession.selectOne(mapperData.getSql(),String.valueOf(args[0]),mapperData.getReturnType());
        }
        return method.invoke(this,args);
    }
}
